﻿using System.Windows;
using Xioa_UI.Controls;


namespace Xioa_UI.Message;

/// <summary>
/// XioaMessageBox.xaml 的交互逻辑
/// </summary>
public partial class XioaMessageBox
{
    public static double MinSizeWidth { get; set; } = 300;
    private XioaMessageBox()
    {
        InitializeComponent();
        this.SizeToContent = SizeToContent.WidthAndHeight;
        this.MinWidth = MinSizeWidth;
    }

    private XioaMessageBox(string header)
    {
        InitializeComponent();
        this.Title = header;
        this.SizeToContent = SizeToContent.WidthAndHeight;
    }

    #region Property

    public string Message
    {
        get => XioaContent.Text;
        set => XioaContent.Text = value;
    }


    public static readonly DependencyProperty HeaderTextProperty =
        ElementBase.Property<XioaMessageBox, string>("HeaderTextProperty", "");

    public string? Header
    {
        get => (string)GetValue(HeaderTextProperty);
        set { SetValue(HeaderTextProperty, value); }
    }

    public Visibility YesVisibility
    {
        get => YesBtn.Visibility;
        set => YesBtn.Visibility = value;
    }

    public object YesText
    {
        get => YesBtn.Content;
        set => YesBtn.Content = value;
    }

    public Visibility CancelVisibility
    {
        get => CancelBtn.Visibility;
        set => CancelBtn.Visibility = value;
    }

    public object CancelText
    {
        get => CancelBtn.Content;
        set => CancelBtn.Content = value;
    }

    public Visibility NoVisibility
    {
        get => NoBtn.Visibility;
        set => NoBtn.Visibility = value;
    }

    public object NoText
    {
        get => NoBtn.Content;
        set => NoBtn.Content = value;
    }

    #endregion

    #region XioaMessageBoxResult

    private XioaMessageBoxResult _messageBoxResult = XioaMessageBoxResult.None;

    #endregion

    #region XioaMessageBox Button Status

    private void ShowBtnStatus(XioaMessageBoxButton xioaMessageBoxButton = XioaMessageBoxButton.Ok)
    {
        switch (xioaMessageBoxButton)
        {
            case XioaMessageBoxButton.Ok:
            {
                YesVisibility = Visibility.Visible;
                NoVisibility = Visibility.Collapsed;
                CancelVisibility = Visibility.Collapsed;
                return;
            }
            case XioaMessageBoxButton.OkCancel:
            {
                YesVisibility = Visibility.Visible;
                NoVisibility = Visibility.Collapsed;
                CancelVisibility = Visibility.Visible;
                return;
            }
            case XioaMessageBoxButton.YesNo:
            {
                YesVisibility = Visibility.Visible;
                NoVisibility = Visibility.Visible;
                CancelVisibility = Visibility.Collapsed;
                return;
            }
            case XioaMessageBoxButton.YesNoCancel:
            {
                YesVisibility = Visibility.Visible;
                NoVisibility = Visibility.Visible;
                CancelVisibility = Visibility.Visible;
                return;
            }
        }
    }

    #endregion

    #region Show message, caption, messageBoxButton

    public static XioaMessageBoxResult Show(
        string message,
        string caption,
        XioaMessageBoxButton messageBoxButton
    )
    {
        var xioaMessageBox = new XioaMessageBox(caption);
        xioaMessageBox.Message = message;
        xioaMessageBox.Header = caption;
        xioaMessageBox.ShowBtnStatus(messageBoxButton);
        xioaMessageBox.ShowDialog();
        return xioaMessageBox._messageBoxResult;
    }

    public static XioaMessageBoxResult Show(
        Window owner,
        string message,
        string caption,
        XioaMessageBoxButton messageBoxButton
    )
    {
        var xioaMessageBox = new XioaMessageBox(caption);
        xioaMessageBox.Message = message;
        xioaMessageBox.Header = caption;
        xioaMessageBox.WindowStartupLocation = WindowStartupLocation.CenterOwner;
        xioaMessageBox.Owner = owner;
        xioaMessageBox.ShowBtnStatus(messageBoxButton);
        xioaMessageBox.ShowDialog();
        return xioaMessageBox._messageBoxResult;
    }

    #endregion

    #region Show message, caption

    public static XioaMessageBoxResult Show(
        string message,
        string caption
    )
    {
        var xioaMessageBox = new XioaMessageBox(caption);
        xioaMessageBox.Message = message;
        xioaMessageBox.Header = caption;
        xioaMessageBox.ShowBtnStatus();
        xioaMessageBox.ShowDialog();
        return xioaMessageBox._messageBoxResult;
    }

    public static XioaMessageBoxResult Show(
        Window owner,
        string message,
        string caption
    )
    {
        var xioaMessageBox = new XioaMessageBox(caption);
        xioaMessageBox.Message = message;
        xioaMessageBox.Header = caption;
        xioaMessageBox.WindowStartupLocation = WindowStartupLocation.CenterOwner;
        xioaMessageBox.Owner = owner;
        xioaMessageBox.ShowBtnStatus();
        xioaMessageBox.ShowDialog();
        return xioaMessageBox._messageBoxResult;
    }

    #endregion

    #region Show message

    public static XioaMessageBoxResult Show(
        string message
    )
    {
        var xioaMessageBox = new XioaMessageBox();
        xioaMessageBox.Message = message;
        // xioaMessageBox.Title = caption;
        xioaMessageBox.ShowBtnStatus();
        xioaMessageBox.ShowDialog();
        return xioaMessageBox._messageBoxResult;
    }

    public static XioaMessageBoxResult Show(
        Window owner,
        string message
    )
    {
        var xioaMessageBox = new XioaMessageBox();
        xioaMessageBox.Message = message;
        // xioaMessageBox.Title = caption;
        xioaMessageBox.ShowBtnStatus();
        xioaMessageBox.WindowStartupLocation = WindowStartupLocation.CenterOwner;
        xioaMessageBox.Owner = owner;
        xioaMessageBox.ShowDialog();
        return xioaMessageBox._messageBoxResult;
    }

    #endregion

    #region XioaMessageBox Button Click

    private void YesBtn_Click(object sender, RoutedEventArgs e)
    {
        this._messageBoxResult = XioaMessageBoxResult.Yes;
        base.Close();
    }

    private void CancelBtn_Click(object sender, RoutedEventArgs e)
    {
        this._messageBoxResult = XioaMessageBoxResult.Cancel;
        base.Close();
    }

    private void NoBtn_Click(object sender, RoutedEventArgs e)
    {
        this._messageBoxResult = XioaMessageBoxResult.No;
        base.Close();
    }

    #endregion

    #region XioaMessageBox ChangeButton Text Show

    public static XioaMessageBoxResult Show(
        string message,
        string caption,
        XioaMessageBoxButton messageBoxButton,
        XioaMessageBoxButtonText? xioaMessageBoxButtonText
    )
    {
        var xioaMessageBox = new XioaMessageBox(caption);
        xioaMessageBox.Message = message;
        xioaMessageBox.Header = caption;
        xioaMessageBox.ShowBtnStatus(messageBoxButton);
        xioaMessageBox.ChangeButtinContent(xioaMessageBoxButtonText);
        xioaMessageBox.ShowDialog();
        return xioaMessageBox._messageBoxResult;
    }

    public static XioaMessageBoxResult Show(
        Window owner,
        string message,
        string caption,
        XioaMessageBoxButton messageBoxButton,
        XioaMessageBoxButtonText? xioaMessageBoxButtonText
    )
    {
        var xioaMessageBox = new XioaMessageBox(caption);
        xioaMessageBox.Message = message;
        xioaMessageBox.Header = caption;
        xioaMessageBox.WindowStartupLocation = WindowStartupLocation.CenterOwner;
        xioaMessageBox.Owner = owner;
        xioaMessageBox.ShowBtnStatus(messageBoxButton);
        xioaMessageBox.ChangeButtinContent(xioaMessageBoxButtonText);
        xioaMessageBox.ShowDialog();
        return xioaMessageBox._messageBoxResult;
    }

    private void ChangeButtinContent(XioaMessageBoxButtonText? xioaMessageBoxButtonText)
    {
        if (xioaMessageBoxButtonText?.No != null)
        {
            this.NoText = xioaMessageBoxButtonText.No;
        }

        if (xioaMessageBoxButtonText?.Yes != null)
        {
            this.YesText = xioaMessageBoxButtonText.Yes;
        }

        if (xioaMessageBoxButtonText?.Cancel != null)
        {
            this.CancelText = xioaMessageBoxButtonText.Cancel;
        }
    }

    #endregion
}